package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.FeedTaskQualityDAO;
import cn.edu.scau.cmi.hombio.dao.PigTypeDAO;
import cn.edu.scau.cmi.hombio.dao.PigstyDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPigstyDAO;

import cn.edu.scau.cmi.hombio.domain.FeedTaskQuality;
import cn.edu.scau.cmi.hombio.domain.PigType;
import cn.edu.scau.cmi.hombio.domain.Pigsty;
import cn.edu.scau.cmi.hombio.domain.Task;
import cn.edu.scau.cmi.hombio.domain.TaskPigsty;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for TaskPigsty entities
 * 
 */

@Service("TaskPigstyService")

@Transactional
public class TaskPigstyServiceImpl implements TaskPigstyService {

	/**
	 * DAO injected by Spring that manages FeedTaskQuality entities
	 * 
	 */
	@Autowired
	private FeedTaskQualityDAO feedTaskQualityDAO;

	/**
	 * DAO injected by Spring that manages PigType entities
	 * 
	 */
	@Autowired
	private PigTypeDAO pigTypeDAO;

	/**
	 * DAO injected by Spring that manages Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyDAO pigstyDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * DAO injected by Spring that manages TaskPigsty entities
	 * 
	 */
	@Autowired
	private TaskPigstyDAO taskPigstyDAO;

	/**
	 * Instantiates a new TaskPigstyServiceImpl.
	 *
	 */
	public TaskPigstyServiceImpl() {
	}

	/**
	 * Load an existing TaskPigsty entity
	 * 
	 */
	@Transactional
	public Set<TaskPigsty> loadTaskPigstys() {
		return taskPigstyDAO.findAllTaskPigstys();
	}

	/**
	 * Return all TaskPigsty entity
	 * 
	 */
	@Transactional
	public List<TaskPigsty> findAllTaskPigstys(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<TaskPigsty>(taskPigstyDAO.findAllTaskPigstys(startResult, maxRows));
	}

	/**
	 * Delete an existing FeedTaskQuality entity
	 * 
	 */
	@Transactional
	public TaskPigsty deleteTaskPigstyFeedTaskQualities(Integer taskpigsty_id, Integer related_feedtaskqualities_id) {
		FeedTaskQuality related_feedtaskqualities = feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(related_feedtaskqualities_id, -1, -1);

		TaskPigsty taskpigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty_id, -1, -1);

		related_feedtaskqualities.setTaskPigsty(null);
		taskpigsty.getFeedTaskQualities().remove(related_feedtaskqualities);

		feedTaskQualityDAO.remove(related_feedtaskqualities);
		feedTaskQualityDAO.flush();

		return taskpigsty;
	}

	/**
	 * Delete an existing TaskPigsty entity
	 * 
	 */
	@Transactional
	public void deleteTaskPigsty(TaskPigsty taskpigsty) {
		taskPigstyDAO.remove(taskpigsty);
		taskPigstyDAO.flush();
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public TaskPigsty saveTaskPigstyTask(Integer id, Task related_task) {
		TaskPigsty taskpigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		taskpigsty.setTask(related_task);
		related_task.getTaskPigsties().add(taskpigsty);
		taskpigsty = taskPigstyDAO.store(taskpigsty);
		taskPigstyDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return taskpigsty;
	}

	/**
	 * Save an existing TaskPigsty entity
	 * 
	 */
	@Transactional
	public void saveTaskPigsty(TaskPigsty taskpigsty) {
		TaskPigsty existingTaskPigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty.getId());

		if (existingTaskPigsty != null) {
			if (existingTaskPigsty != taskpigsty) {
				existingTaskPigsty.setId(taskpigsty.getId());
				existingTaskPigsty.setMemo(taskpigsty.getMemo());
			}
			taskpigsty = taskPigstyDAO.store(existingTaskPigsty);
		} else {
			taskpigsty = taskPigstyDAO.store(taskpigsty);
		}
		taskPigstyDAO.flush();
	}

	/**
	 * Return a count of all TaskPigsty entity
	 * 
	 */
	@Transactional
	public Integer countTaskPigstys() {
		return ((Long) taskPigstyDAO.createQuerySingleResult("select count(o) from TaskPigsty o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Pigsty entity
	 * 
	 */
	@Transactional
	public TaskPigsty deleteTaskPigstyPigsty(Integer taskpigsty_id, Integer related_pigsty_id) {
		TaskPigsty taskpigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty_id, -1, -1);
		Pigsty related_pigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsty_id, -1, -1);

		taskpigsty.setPigsty(null);
		related_pigsty.getTaskPigsties().remove(taskpigsty);
		taskpigsty = taskPigstyDAO.store(taskpigsty);
		taskPigstyDAO.flush();

		related_pigsty = pigstyDAO.store(related_pigsty);
		pigstyDAO.flush();

		pigstyDAO.remove(related_pigsty);
		pigstyDAO.flush();

		return taskpigsty;
	}

	/**
	 * Save an existing Pigsty entity
	 * 
	 */
	@Transactional
	public TaskPigsty saveTaskPigstyPigsty(Integer id, Pigsty related_pigsty) {
		TaskPigsty taskpigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(id, -1, -1);
		Pigsty existingpigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsty.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigsty != null) {
			existingpigsty.setId(related_pigsty.getId());
			existingpigsty.setNumber(related_pigsty.getNumber());
			existingpigsty.setRfid(related_pigsty.getRfid());
			existingpigsty.setCapacity(related_pigsty.getCapacity());
			existingpigsty.setMemo(related_pigsty.getMemo());
			related_pigsty = existingpigsty;
		}

		taskpigsty.setPigsty(related_pigsty);
		related_pigsty.getTaskPigsties().add(taskpigsty);
		taskpigsty = taskPigstyDAO.store(taskpigsty);
		taskPigstyDAO.flush();

		related_pigsty = pigstyDAO.store(related_pigsty);
		pigstyDAO.flush();

		return taskpigsty;
	}

	/**
	 * Save an existing FeedTaskQuality entity
	 * 
	 */
	@Transactional
	public TaskPigsty saveTaskPigstyFeedTaskQualities(Integer id, FeedTaskQuality related_feedtaskqualities) {
		TaskPigsty taskpigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(id, -1, -1);
		FeedTaskQuality existingfeedTaskQualities = feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(related_feedtaskqualities.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfeedTaskQualities != null) {
			existingfeedTaskQualities.setId(related_feedtaskqualities.getId());
			existingfeedTaskQualities.setMemo(related_feedtaskqualities.getMemo());
			related_feedtaskqualities = existingfeedTaskQualities;
		} else {
			related_feedtaskqualities = feedTaskQualityDAO.store(related_feedtaskqualities);
			feedTaskQualityDAO.flush();
		}

		related_feedtaskqualities.setTaskPigsty(taskpigsty);
		taskpigsty.getFeedTaskQualities().add(related_feedtaskqualities);
		related_feedtaskqualities = feedTaskQualityDAO.store(related_feedtaskqualities);
		feedTaskQualityDAO.flush();

		taskpigsty = taskPigstyDAO.store(taskpigsty);
		taskPigstyDAO.flush();

		return taskpigsty;
	}

	/**
	 * Save an existing PigType entity
	 * 
	 */
	@Transactional
	public TaskPigsty saveTaskPigstyPigType(Integer id, PigType related_pigtype) {
		TaskPigsty taskpigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(id, -1, -1);
		PigType existingpigType = pigTypeDAO.findPigTypeByPrimaryKey(related_pigtype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigType != null) {
			existingpigType.setId(related_pigtype.getId());
			existingpigType.setName(related_pigtype.getName());
			existingpigType.setMemo(related_pigtype.getMemo());
			related_pigtype = existingpigType;
		} else {
			related_pigtype = pigTypeDAO.store(related_pigtype);
			pigTypeDAO.flush();
		}

		taskpigsty.setPigType(related_pigtype);
		related_pigtype.getTaskPigsties().add(taskpigsty);
		taskpigsty = taskPigstyDAO.store(taskpigsty);
		taskPigstyDAO.flush();

		related_pigtype = pigTypeDAO.store(related_pigtype);
		pigTypeDAO.flush();

		return taskpigsty;
	}

	/**
	 */
	@Transactional
	public TaskPigsty findTaskPigstyByPrimaryKey(Integer id) {
		return taskPigstyDAO.findTaskPigstyByPrimaryKey(id);
	}

	/**
	 * Delete an existing PigType entity
	 * 
	 */
	@Transactional
	public TaskPigsty deleteTaskPigstyPigType(Integer taskpigsty_id, Integer related_pigtype_id) {
		TaskPigsty taskpigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty_id, -1, -1);
		PigType related_pigtype = pigTypeDAO.findPigTypeByPrimaryKey(related_pigtype_id, -1, -1);

		taskpigsty.setPigType(null);
		related_pigtype.getTaskPigsties().remove(taskpigsty);
		taskpigsty = taskPigstyDAO.store(taskpigsty);
		taskPigstyDAO.flush();

		related_pigtype = pigTypeDAO.store(related_pigtype);
		pigTypeDAO.flush();

		pigTypeDAO.remove(related_pigtype);
		pigTypeDAO.flush();

		return taskpigsty;
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public TaskPigsty deleteTaskPigstyTask(Integer taskpigsty_id, Integer related_task_id) {
		TaskPigsty taskpigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(taskpigsty_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		taskpigsty.setTask(null);
		related_task.getTaskPigsties().remove(taskpigsty);
		taskpigsty = taskPigstyDAO.store(taskpigsty);
		taskPigstyDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return taskpigsty;
	}
}
